package utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public final class Buffer {
	//private static Arquivo file = new Arquivo( null, "Saida.txt");
	private static Arquivo file;
	private static HashMap<Object, String> hash;
	//private static String buffer = "";
	
	/*
	 * filePath: Path to create the file
	 * */
	public static void setFile(String filePath){
		if(file != null){
			file.close();
		}
		file = new Arquivo(null, filePath);
		hash = new HashMap<Object, String>();
	}
	
	public static void println(String str){
		file.println(str);
	}
	
	public static void print(String str){
		file.print(str);
	}
	
	public static void printOutHash(){
		System.out.println(hash.toString());
	}
	
	public static void insertHashToFile(Object key){
		String value = hash.get(key);
		println(String.valueOf(key)+", "+value);
	}
	
	public static void insertAllHashToFile(){
		Set<Object> keys = hash.keySet();
		Iterator<Object> i = keys.iterator();
		
		while(i.hasNext()){
			Object obj = i.next();
			String entry = obj+"; "+hash.get(obj);
			file.println(entry);
			System.out.println(entry);
		}
		/*Set<Entry<Object, String>> keys = hash.entrySet();
		Iterator<Entry<Object, String>> i = keys.iterator();
		
		while(i.hasNext()){
			System.out.println(i.next());
		}*/
	}
	
	public static String getHashValue(Object key){
		return hash.get(key);
	}
	
	public static void insertHashBuffer(Object key, String value){
		hash.put(key, value);
	}
	
	public static String getHashRepresentation(){
		return hash.toString();
	}
	
	public static void close(){
		file.close();
		if(hash != null){
			hash.clear();
		}
	}
}
